package hot.linklist;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class A142 {


     class ListNode {
         int val;
      ListNode next;
     ListNode(int x) {
         val = x;
         next = null;
    }
 }
    public ListNode detectCycle(ListNode head) {
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
 fast=fast.next.next;
    slow=slow.next;
  if(fast==slow){
      //相遇
      //再走a步就到达入环点
      while(head!=slow){
          head=head.next;
          slow=slow.next;
      }
      return slow;
  }
}
return null;
    }
}
